/*
 * Copyright (C) 2004, Velocityme Partnership
 * ShowAccessAction.java
 *
 * Created on 29 August 2004, 12:03
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.PermissionBean;
//web imports
import com.velocityme.www.dto.*;
import com.velocityme.www.actionforms.*;
import com.velocityme.www.utility.NodeUtil;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;

import org.apache.commons.beanutils.*;
//ejb interfaces
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.GroupAccessValue;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.UserAccessValue;

/**
 *
 * @author  Robert
 */
public class ShowAccessAction extends org.apache.struts.action.Action {
    
    /** Creates a new instance of ShowAccessAction */
    public ShowAccessAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, 
        ActionForm form,  HttpServletRequest request, HttpServletResponse response) throws Exception {
        try{
            NodeIdActionForm node = (NodeIdActionForm)form;
            HttpSession session = request.getSession();
            KeySession key = (KeySession)session.getAttribute("sessionKey");
            RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
            NodePK nodePK = new NodePK(new Integer(node.getNodeId()));
            NodeValue nodeValue = cs.getNodeValue(key, nodePK);
            if (nodeValue.getType().equals("Group")) {
                GroupPK groupPK = new GroupPK(nodePK.getNodeId());
                session.setAttribute("groupValue", cs.getGroupValue(key, groupPK));
                Iterator i = cs.getGroupAccessByUserValue(key, groupPK).iterator();
                Collection accessDTOs = new ArrayList();
                PermissionPK permissionGroupAccessDelete = new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_DELETE));
                while(i.hasNext()) {
                    GroupAccessValue groupAccessValue = (GroupAccessValue)i.next();
                    Boolean isDeletable = new Boolean(cs.getAssignedPermissions(key, groupAccessValue.getNodeValue().getPrimaryKey()).contains(permissionGroupAccessDelete));
		    Integer groupAccessId = groupAccessValue.getGroupAccessId();
                    String nodeName = groupAccessValue.getNodeValue().getName();
                    String groupName = groupAccessValue.getGroupValue().getNodeValue().getName();
                    String roleName = groupAccessValue.getRoleValue().getNodeValue().getName();
                    accessDTOs.add(new AccessDTO(isDeletable, groupAccessId, nodeName, groupName, roleName));
                }
                session.setAttribute("groupAccessByGroup", accessDTOs);
                ShowNodeAction.setupTreeDisplay(session, nodeValue, "My Access");
                return mapping.findForward("ACTION_SHOW_GROUP_SUCCESS");
            }
            if (nodeValue.getType().equals("User")) {
                UserPK userPK = new UserPK(nodePK.getNodeId());
                session.setAttribute("userValue", cs.getUserValue(key, userPK));
                Iterator i = cs.getUserAccessByUserValue(key, userPK).iterator();
                Collection accessDTOs = new ArrayList();
                PermissionPK permissionUserAccessDelete = new PermissionPK(new Integer(PermissionBean.USER_ACCESS_DELETE));
                while(i.hasNext()) {
                    UserAccessValue userAccessValue = (UserAccessValue)i.next();
                    Boolean isDeletable = new Boolean(cs.getAssignedPermissions(key, userAccessValue.getNodeValue().getPrimaryKey()).contains(permissionUserAccessDelete));
		    Integer userAccessId = userAccessValue.getUserAccessId();
                    String nodeName = userAccessValue.getNodeValue().getName();
                    String userName = userAccessValue.getUserValue().getNodeValue().getName();
                    String roleName = userAccessValue.getRoleValue().getNodeValue().getName();
                    accessDTOs.add(new AccessDTO(isDeletable, userAccessId, nodeName, userName, roleName));
                }
                session.setAttribute("userAccessByUser", accessDTOs);
                ShowNodeAction.setupTreeDisplay(session, nodeValue, "My Access");
                return mapping.findForward("ACTION_SHOW_USER_SUCCESS");
            }
        }
        catch(Exception e){
            request.setAttribute("Additional_Info", e);
        }
        return mapping.findForward("ACTION_SHOW_ACCESS_FAILED");
    }
    
}
